package com.cn.service.server.PartsManagement.Impl;

import com.cn.bean.pojo.PartsManagement.BusinessTypeTable;
import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.bean.pojo.sys.System_parameter_sub_table;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.PartsManagement.BusinessTypeDao;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.PartsManagement.BusinessTypeService;
import com.cn.service.server.PartsManagement.ComponentItemClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BusinessTypeServiceImpl implements BusinessTypeService {
    private final BusinessTypeDao businessTypeDao;
    private final Sys_Sequence_Info_TDAO sysSequenceInfoTService;
    @Autowired(required = false)
    public BusinessTypeServiceImpl(BusinessTypeDao businessTypeDao, Sys_Sequence_Info_TDAO sysSequenceInfoTService) {
        this.businessTypeDao = businessTypeDao;
        this.sysSequenceInfoTService = sysSequenceInfoTService;
    }

    @Override
    public List<BusinessTypeTable> selectBusinessTypeAsideAuto1() {
        return businessTypeDao.selectBusinessTypeAsideAuto1();
    }

    @Override
    public List<BusinessTypeTable> selectBusinessTypeAside() {
        return businessTypeDao.selectBusinessTypeAside();
    }

    @Override
    public List<BusinessTypeTable> selectBusinessType(int limit, int page) {
        return businessTypeDao.selectBusinessType(limit, page);
    }

    @Override
    public List<System_parameter_sub_table> selectSystem_parameter_sub_table(String tableName, String field) {
        return businessTypeDao.selectSystem_parameter_sub_table(tableName, field);
    }

    @Override
    public Result addBusinessType(String name, String description, int status, int level, int parentID, int isLeaf) {
        List<BusinessTypeTable> businessTypeTables = businessTypeDao.selectBusinessTypeAside();
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTService.selectSys_Sequence_Info_T("BusinessTypeTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {

            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTService.updateSys_Sequence_Info_T(ID, "BusinessTypeTable");
        for (BusinessTypeTable businessType : businessTypeTables) {
            if (businessType.getName().equals(name)) {
                return Result.failure("500", "业务类型名称重复");
            }
        }
        int i = businessTypeDao.addBusinessType(ID, code, name, description, status, level, parentID, isLeaf);
        if (i == 0) {
            return Result.failure("500", "添加失败");
        }
        return Result.success("200", "添加成功");
    }

    @Override
    public Result deleteBusinessType(int ID) {
        List<BusinessTypeTable> businessTypeTables = businessTypeDao.selectBusinessTypeAside();
        boolean hasChildren = hasChildren(businessTypeTables, ID);
        if (hasChildren) {
            return Result.failure("500", "删除失败，当前节点存在子节点");
        }
        int i = businessTypeDao.deleteBusinessType(ID);
        if (i > 0) {
            return Result.success("200", "删除成功");
        }
        return Result.failure("500", "删除失败");
    }

    @Override
    public Result upDateBusinessType(int ID, String code, String name, String description, int status, int level, int parentID, int isLeaf) {
        List<BusinessTypeTable> businessTypeTables = businessTypeDao.selectBusinessTypeAside();

        for (BusinessTypeTable businessType : businessTypeTables) {
            if (businessType.getID() != ID && businessType.getName().equals(name)) {
                return Result.failure("500", "业务类型名称重复");
            }
        }
        int i = businessTypeDao.upDateBusinessType(ID, code, name, description, status, level, parentID, isLeaf);

        if (i > 0) {
            return Result.success("200", "修改成功");
        }
        return Result.failure("500", "修改失败");
    }

    private boolean hasChildren(List<BusinessTypeTable> nodes, int id) {
        for (BusinessTypeTable node : nodes) {
            if (node.getParentID() == id) {
                return true;
            }
        }
        return false;
    }

}
